package myPack;

import javax.swing.*;

import java.awt.Button;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextComponent;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileFilter;

import org.farng.mp3.TagException;

import java.awt.Color;
import java.awt.GridLayout;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

public class GUI {

	private Frame window;
	private JPanel panel;
	private JButton but_Browse;
	private JButton but_Sort;
	private JButton btnSelectFile;
	
	private File selectedFile;
	private File selectedFolder = null;
	private JLabel lblSelectedFile;
	private JButton btnShowTags;
	private JTextArea textArea;
	private JScrollPane scrollPane;
	private JTable table;
	
	public GUI() {
		window = new Frame("MP3Sorter");
		//window.setSize(158, 125);
		//window.setLocation(300, 300);
		window.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				System.exit(0);
			}
		});
		
		
		panel = new JPanel();
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				ColumnSpec.decode("67px:grow"),
				ColumnSpec.decode("53px"),
				ColumnSpec.decode("1px"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				RowSpec.decode("189px:grow"),
				RowSpec.decode("23px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		window.add(panel);
		but_Browse = new JButton("Browse");
		but_Browse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final JFileChooser fc = new JFileChooser();
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int retval = fc.showOpenDialog(window);
				if (retval == JFileChooser.APPROVE_OPTION) {
					selectedFolder = fc.getSelectedFile();
					addFolder(selectedFolder);
				}
			}
		});
		
		scrollPane = new JScrollPane();
		panel.add(scrollPane, "1, 1, 5, 18, fill, fill");
		
		table = new JTable(new MyTableModel());
		table.setFillsViewportHeight(true);
		scrollPane.setViewportView(table);
		
		panel.add(but_Browse, "1, 20, 2, 1, left, center");
		
		btnSelectFile = new JButton("Select File");
		btnSelectFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				final JFileChooser fc = new JFileChooser();
				int retval = fc.showOpenDialog(window);
				if (retval == JFileChooser.APPROVE_OPTION) {
					selectedFile = fc.getSelectedFile();
					lblSelectedFile.setText(selectedFile.toString());
				}
			}
		});
		
		but_Sort = new JButton("Sort");
		panel.add(but_Sort, "5, 20, left, center");
		
		textArea = new JTextArea();
		panel.add(textArea, "7, 20, 5, 3, fill, fill");
		panel.add(btnSelectFile, "1, 22, 2, 1");
		
		btnShowTags = new JButton("Show Tags");
		btnShowTags.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Song song = new Song(selectedFile);
				song.listTags(textArea);
			}
		});
		panel.add(btnShowTags, "5, 22");
		
		lblSelectedFile = new JLabel("selected File");
		panel.add(lblSelectedFile, "13, 22");
		
		window.pack();
		window.setVisible(true);
		
	}
	
	private void addFolder(File folder) {
		for (String filename: folder.list()) {
			String fullPath = folder.getAbsolutePath() + "\\" + filename;
			File file = new File(fullPath);
			//File testfile = new File(test);
			if (file.isDirectory()) {
				addFolder(file);
			} else {
				if (file.getName().endsWith(".mp3") || file.getName().endsWith(".MP3")) {
					Song song = new Song(file);
					((MyTableModel)table.getModel()).addRow(song);
				}
			}
		}
	}
	
	
	private void showAlertWindow(String text) {
		JFrame frame = new JFrame("Achtung!");
		Panel pan = new Panel();
		Button but = new Button("Ok");
		
		but.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				((JFrame)((Panel)((Button)arg0.getSource()).getParent()).getParent()).dispose();
			}
		});
		
		pan.add(new Label(text));
		pan.add(but);
		frame.getContentPane().add(pan);
		frame.pack();
		frame.setVisible(true);
		
	}
	
	public void changeTableAt(Object data, int row, int col) {
		this.table.setValueAt(data, row, col);
	}

}
